package com.ruoyi.psi.purchase.pay.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.purchase.pay.domain.PurchasePay;
import com.ruoyi.psi.purchase.pay.domain.PurchasePayList;
import com.ruoyi.psi.purchase.pay.service.IPurchasePayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购付款/委外付款
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.17
 */
@RestController
@RequestMapping("/psi/purchase/pay")
@Api(value = "采购付款模块", tags = "采购付款接口")
public class PurchasePayController extends BaseController {
    @Autowired
    private IPurchasePayService purchasePayService;
    /**
     * 获取采购订单
     * @param purchasePay
     * @return
     */
    @ApiOperationSupport(includeParameters = {"PurchasePay.purchasePayId", "PurchasePay.supplierInfoId", "PurchasePay.payTime"})
    @ApiOperation(value = "获取采购订单列表",notes = "查询所有采购订单信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PurchasePay purchasePay) {
        startPage();
        List<PurchasePay> purchasePays = purchasePayService.selectPurchasePayList(purchasePay);
        return getDataTable(purchasePays);
    }

    /**
     * 导出采购订单
     *
     * @param purchasePay
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody PurchasePay purchasePay) {
        List<PurchasePay> List = purchasePayService.selectPurchasePayList(purchasePay);
        ExcelUtil<PurchasePay> util = new ExcelUtil<PurchasePay>(PurchasePay.class);
        return util.exportExcel(List, "采购订单单数据");
    }

    /**
     * 新增付款单时查询对应采购订单、采购退货
     *
     * @param purchasePay
     * @return
     */
    @PostMapping("/getPayBill")
    public AjaxResult getPayBill(@RequestBody PurchasePay purchasePay) {
        List<PurchasePayList> List = purchasePayService.getPayBill(purchasePay.getSupplierInfoId());
        return AjaxResult.success(List);
    }
    /**
     * 新增采购订单
     * @param purchasePay 采购订单单据
     * @return
     */
    @ApiOperation(value = "新增采购订单", notes = "新增采购订单信息")
    @ApiImplicitParam(name = "purchasePay", value = "新增采购订单(参数：purchasePayId，supplierInfoId，payTime，discountAmount，remark)", dataType = "PurchasePay")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PurchasePay purchasePay) {
        try {
            purchasePayService.insertPurchasePay(purchasePay);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新采购订单
     * @param purchasePay 采购订单单据
     * @return
     */
    @ApiOperation(value = "更新采购订单", notes = "更新采购订单")
    @ApiImplicitParam(name = "purchasePay", value = "更新采购订单", dataType = "PurchasePay")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody PurchasePay purchasePay) {
        try {
            purchasePayService.updatePurchasePay(purchasePay);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除采购订单
     * 仅修改删除标志位
     *
     * @param purchasePay 采购订单单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody PurchasePay purchasePay) {

        return toAjax(purchasePayService.updatePurchasePayDeleteFlag(purchasePay));
    }
}
